package city.spring.modules.reception.service.impl;

import city.spring.modules.reception.entity.OldmanServiceRecordEntity;
import city.spring.modules.reception.entity.OldmanVolunteerInfoEntity;
import city.spring.modules.reception.repository.OldmanServiceRecordRepository;
import city.spring.modules.reception.service.OldmanServiceRecordService;
import city.spring.modules.reception.service.OldmanVolunteerInfoService;
import city.spring.modules.reception.vo.OldmanServiceRecordVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * Service：志愿者服务记录
 *
 * @author pxm
 * @date 2020-07-01 11:16:48
 */
@CacheConfig(cacheNames = {OldmanServiceRecordServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class OldmanServiceRecordServiceImpl extends ServiceImpl<OldmanServiceRecordRepository, OldmanServiceRecordEntity> implements OldmanServiceRecordService {

    private final UserService userService;
    private final OldmanVolunteerInfoService oldmanVolunteerInfoService;
    private final OldmanServiceRecordRepository oldmanServiceRecordRepository;

    public OldmanServiceRecordServiceImpl(UserService userService, OldmanVolunteerInfoService oldmanVolunteerInfoService, OldmanServiceRecordRepository oldmanServiceRecordRepository) {
        this.userService = userService;
        this.oldmanVolunteerInfoService = oldmanVolunteerInfoService;
        this.oldmanServiceRecordRepository = oldmanServiceRecordRepository;
    }

    @Override
    public void saveOldmanServiceRecord(OldmanServiceRecordEntity entity, @NonNull Principal user) {
        // 当前记录人
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setRecorder(userEntity.getId());
        // 联系方式
        OldmanVolunteerInfoEntity oldmanVolunteerInfoEntity = oldmanVolunteerInfoService.getById(entity.getVolunteer());
        entity.setContactNum(oldmanVolunteerInfoEntity.getPhone());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanServiceRecord(OldmanServiceRecordEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void updateOldmanServiceRecordByVolunteer(OldmanServiceRecordEntity entity) {
        // 使用 updateByVolunteer(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        oldmanServiceRecordRepository.updateByVolunteer(entity);

    }

    @Override
    public void deleteOldmanServiceRecord(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanServiceRecord(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<OldmanServiceRecordVO> listPage(Page<? extends OldmanServiceRecordEntity> page, Wrapper<? extends OldmanServiceRecordEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}